<?php

namespace Addons\GiveReward\Controller;

use Home\Controller\AddonsController;
use Admin\Builder\AdminListBuilder;
use Admin\Builder\AdminConfigBuilder;
use Admin\Builder\AdminTreeListBuilder;
use Think\Controller;


class GiveRewardController extends AddonsController
{
    /**
     * 获取管理员配置参数
     */
    public function eject()
    {
        $user = M('Member')->where(array('uid' => is_login()))->find();
        $config= S('reward_config');
        if(!$config){
            $config = M('Addons')->where(array('name' => 'GiveReward'))->find();
            S('reward_config', $config, 400);
        }
        $config = json_decode($config['config'], true);
        $min = 0.01;
        $rand = mt_rand($min * 100, $config['limitGiveReward'] * 100) / 100;

        parse_str(I('param'), $param);


        if(!$param['url']){  //当是伪静态状态下，拼接数组，变成非伪静态数组。
            $getdata=$_GET;
            $getdata['user_id']=preg_replace("/.html+/si","",$getdata['user_id']);
            $param=array('type'=>$param['type'],'url'=>$getdata['url'],'user_id'=>$getdata['user_id']);
        }

        $isGive = M('GiveReward')->where(array('hook_type' => $param['type'], 'url' => $param['url'], 'uid' => is_login()))->find();
        $field= S('field');
        if(!$field){
            $field = D('Ucenter/Score')->getTypeList(array('status' => 1));
            S('field', $field, 400);
        }

        foreach ($field as &$v) {
            $v['score'] = $user['score' . $v['id']];
            if ($isGive['field'] == $v['id']) {
                $isGive['field'] = $v;
            }
        }
        if ($isGive) {
            $this->assign('myGive', $isGive);
        } else {
            $this->assign('myGive', 0);
        }

        $this->assign('param', $param);
        $this->assign("rand", $rand);
        $this->assign("field", $field);
        $this->display(T('Addons://GiveReward@giveReward/eject'));
    }


    public function doGiveReward()
    {
       $aType=I('post.type','','op_t');
        $aUrl=I('post.url','','op_t');
        $aToUserId=I('post.user_id','','op_t');

       $param=array('type'=>$aType,'url'=>$aUrl);

        $aField = I('post.field', '', 'op_t');
        $aNum = I('post.num', '', 'op_t');
        if ($aNum < 0) {
            $this->error('打赏不能为负！');
        }
        if(preg_match("/[\x7f-\xff]/", $aNum)){
            $this->error('请输入数字');
        }
        $y=explode(".",$aNum);
        if(   strlen($y[1])>2){
            $this->error('小数点后不能超过两位');
        }
        $data['uid'] = is_login();
        $data['url'] = $param['url'] ? $param['url'] : '';
        $data['field'] = $aField;
        $data['num'] = $aNum;
        $data['to_uid'] = $aToUserId;

        if( $data['to_uid']==is_login()){
            $this->error('不能对自己进行打赏！');
        }
       // $data['hook_data'] = json_encode($param['data']) ? json_encode($param['data']) : '';
        $data['hook_type'] = $param['type'];
        $data['create_time'] = time();

        $field= S('field');
        if(!$field){
            $field = D('Ucenter/Score')->getTypeList(array('status' => 1));
            S('field', $field, 400);
        }
            $user = D('Member')->where(array('uid' => is_login()))->find();
            foreach ($field as &$v) {
                if ($aField == $v['id']) {
                    if (($user['score' . $aField] - $aNum) < 0) {
                        $this->error('你的' . $v['title'] . '不足，打赏失败。');
                    }else{
                        $res = M('GiveReward')->add($data);
                        if($res){
                            action_log('give_reward', 'give_reward', $res, is_login());
                            M('Member')->where(array('uid' => is_login()))->setDec('socre'.$v['id'], $aNum);
                            D('Message')->sendMessageWithoutCheckSelf(is_login(), '您打赏了个' . $param['type'] , $v['title'] . '减少了' . $aNum.$v['unit'],$data['url']);
                            action_log('get_reward', 'give_reward', $res,$aToUserId);
                            M('Member')->where(array('uid' =>$aToUserId))->setInc('socre'.$v['id'], $aNum);
                            D('Message')->sendMessageWithoutCheckSelf($aToUserId, '你获得了一个打赏' . $param['type'] , $v['title'] . '增加了' . $aNum.$v['unit'],$data['url']);
                            $this->success('打赏成功！');
                        }else{
                            $this->error('打赏失败！');
                        }
                    }
                }
            }
    }

    public function getRecord()
    {
        $aUrl = I('post.url_id', '', 'op_t');
        $aType = I('post.type', '', 'op_t');
        $aPage = I('post.page', '', 'op_t');

        $record = M('GiveReward')->where(array('url' => $aUrl, 'type' => $aType))->page($aPage, 10)->order('create_time desc')->select();

        foreach ($record as &$v) {
            $v['user'] = query_user(array('nickname', 'avatar32', 'space_url'), $v['uid']);
        }
        if ($record) {
            $data['html'] = "";
            foreach ($record as $val) {
                $this->assign("v2", $val);
                $data['html'] .= $this->fetch(T("Addons://GiveReward@giveReward/_record"));
                $data['status'] = 1;
            }
        } else {
            $data['stutus'] = 0;
            if ($aPage == 1) {
                $data['info'] = "还未有人打赏！";
            } else {
                $data['info'] = "没有更多了！";
            }

        }

        $this->ajaxReturn($data);
    }


}